<?php

namespace common\models;

use Yii;
use wbraganca\behaviors\NestedSetBehavior;
use wbraganca\behaviors\NestedSetQuery; 


/**
 * This is the model class for table "category_company".
 *
 * @property string $id_category_company
 * @property integer $id_company
 * @property integer $root
 * @property string $lft
 * @property string $rgt
 * @property integer $level
 * @property string $name
 * @property integer $parents
 * @property string $created
 * @property integer $order
 *
 * @property Company $idCompany
 */
class CategoryCompany extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'category_company';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_company', 'root', 'lft', 'rgt', 'level', 'parents', 'order'], 'integer'],
            [['name', 'id_company'], 'required'],
            [['created'], 'safe'],
            [['name'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_category_company' => 'Id Category Company',
            'id_company' => 'Id Company',
            'root' => 'Root',
            'lft' => 'Lft',
            'rgt' => 'Rgt',
            'level' => 'Level',
            'name' => 'Name',
            'parents' => 'Parents',
            'created' => 'Created',
            'order' => 'Order',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getIdCompany()
    {
        return $this->hasOne(Company::className(), ['id_company' => 'id_company']);
    }
    
    /*
     * add behaviors
     */
    public function behaviors()
    {
        return [
            [
                'class' => NestedSetBehavior::className(),
                'idAttribute' => 'id_category_company',
                'titleAttribute' => 'name'
                // 'rootAttribute' => 'root',
                // 'levelAttribute' => 'level',
                // 'hasManyRoots' => true
            ],
        ];
    }

    /**
     * override
     * @return NestedSetQuery
     */
    public static function find()
    {
        return new NestedSetQuery(get_called_class());
    }
    
    /**
     * get list category
     */
    public static function getListCategory($parent,$stop = 0,$break = true){
        $result = array();
        self::loopNode($result, $parent, $stop,""," - -",$break);
        return $result;
    }
    
    /**
     *
     * @param type $result
     * @param type $parent
     * @param type $stop
     * @return type 
     */
    public static function loopNode(&$result,$parent,$stop,$prefix,$separ = " - -",$break = false){
        if($stop != 0 && $parent == $stop)
            return true;
        else{
            $category = self::findOne($parent);
            if(!$break){
                $result[] = array(
                    'id' => $category->id_category_company,
                    'name' => $prefix." ".$category->name
                );
            }
            foreach($category->children()->orderBy('order')->all() as $item){
                self::loopNode($result, $item->id_category_company, $stop,$prefix.$separ,$separ);
            }
        }
    }
}
